﻿using System;
using System.Collections.Generic;
using System.Text;
using Wangzeran.CodeMaker.Core.Tag;

namespace Wangzeran.CodeMaker.PlugIns.Tags
{
    /// <summary>
    /// 处理模板标签
    /// </summary>
    public class TemplateTag : TagSupport
    {

        #region 属性
        /// <summary>
        /// 模板名称
        /// </summary>
        private const string ATTR_NAME = "name";
        /// <summary>
        /// 模板作者
        /// </summary>
        private const string ATTR_AUTHOR = "author";
        /// <summary>
        /// 模板版本
        /// </summary>
        private const string ATTR_VERSION = "version";
        /// <summary>
        /// 模板类型
        /// </summary>
        private const string ATTR_TYPE = "type";
        /// <summary>
        /// 模板语言
        /// </summary>
        private const string ATTR_LANGUAGE = "language";

        #endregion

        /// <summary>
        /// 处理模板
        /// </summary>
        /// <param name="sb"></param>
        public delegate void ProcessTemplateHandler(StringBuilder sb);

        /// <summary>
        /// 模板集合
        /// </summary>
        private IDictionary<string, ProcessTemplateHandler> dicTemplates;
        /// <summary>
        /// 获取模板集合
        /// </summary>
        public IDictionary<string, ProcessTemplateHandler> DicTemplates
        {
            get 
            {
                if (dicTemplates == null)
                {
                    dicTemplates = new Dictionary<string, ProcessTemplateHandler>();
                    dicTemplates.Add("csfile", ProcessCSFile);
                    dicTemplates.Add("xml", ProcessXmlFile);
                }
                return dicTemplates;
            }
        }

        /// <summary>
        /// 标签名称
        /// </summary>
        public override string TagName
        {
            get { return "c:template"; }
        }

        /// <summary>
        /// 处理标签
        /// </summary>
        /// <param name="tagStr">标签文本</param>
        /// <param name="value">标签值</param>
        /// <returns></returns>
        public override string ProcessTag(string tagStr, object value)
        {
            try
            {
                StringBuilder sb = new StringBuilder();

                if (!this.Properties.ContainsKey(ATTR_TYPE))
                {
                    return String.Empty;
                }

                string type = this.Properties[ATTR_TYPE].ToLower();
                if (DicTemplates.ContainsKey(type))
                {
                    DicTemplates[type].Invoke(sb);
                }

                return sb.ToString();
            }
            catch (Exception ex)
            {	
                throw ex;
            }

        }

        /// <summary>
        /// 处理CSFile
        /// </summary>
        /// <param name="sb">StringBuilder</param>
        private void ProcessCSFile(StringBuilder sb)
        {
            try
            {
                sb.AppendLine("//");
                sb.AppendLine("// Generate By CodeMaker");
                sb.AppendLine("//");
                if (this.Properties.ContainsKey(ATTR_NAME))
                {
                    sb.AppendLine("// 模板名称：" + this.Properties[ATTR_NAME]);
                }
                if (this.Properties.ContainsKey(ATTR_AUTHOR))
                {
                    sb.AppendLine("// 模板作者：" + this.Properties[ATTR_AUTHOR]);
                }
                if (this.Properties.ContainsKey(ATTR_VERSION))
                {
                    sb.AppendLine("// 模板版本：v" + this.Properties[ATTR_VERSION]);
                }
                sb.Append("//");
            }
            catch (Exception ex)
            {	
                throw ex;
            }
        }

        /// <summary>
        /// 处理XmlFile
        /// </summary>
        /// <param name="sb">StringBuilder</param>
        private void ProcessXmlFile(StringBuilder sb)
        {
            try
            {
                sb.AppendLine("<!--");
                sb.AppendLine("//");
                sb.AppendLine("// Generate By CodeMaker");
                sb.AppendLine("//");
                if (this.Properties.ContainsKey(ATTR_NAME))
                {
                    sb.AppendLine("// 模板名称：" + this.Properties[ATTR_NAME]);
                }
                if (this.Properties.ContainsKey(ATTR_AUTHOR))
                {
                    sb.AppendLine("// 模板作者：" + this.Properties[ATTR_AUTHOR]);
                }
                if (this.Properties.ContainsKey(ATTR_VERSION))
                {
                    sb.AppendLine("// 模板版本：v" + this.Properties[ATTR_VERSION]);
                }
                sb.AppendLine("//");
                sb.Append("-->");
            }
            catch (Exception ex)
            {	
                throw ex;
            }
        }
    }
}
